@isTest(SeeAllData=true)
private class SignatureControllerTest {
    static testMethod void testMethod1() {
        //TestUtils.createUsers();

        Id ldcProfile = [Select Id From Profile Where Name ='系统管理员' or Name='System Administrator'].Id;
        Id engProfile = [Select Id From Profile Where Name ='工程部工程师'].Id;
        User u1 = new User( Alias = 'samLdc1', 
                         Email = 'ldc@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户1', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = ldcProfile, 
                         Country = 'United States',
                         IsActive = true,
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123451',
                         CompanyName = 'Y-MKSY',
                         Province__c = '四川省',
                         FederationIdentifier = '123451',
                         Username = 'Matldc1@test.com.maike' );
        
        User u2 = new User( Alias = 'samLdc2', 
                Email = 'ldc@test.com.maike',
                Emailencodingkey = 'UTF-8', 
                Lastname = '用户2', 
                Languagelocalekey = 'en_US',
                Localesidkey = 'en_US', 
                Profileid = ldcProfile, 
                Country = 'United States',
                IsActive = true,
                UserDepartment__c='客户服务中心(产品培训部)',
                Timezonesidkey = 'America/Los_Angeles', 
                EmployeeNumber = '123452',
                CompanyName = 'Y-MKSY',
                Province__c = '四川省',
                FederationIdentifier = '123452',
                Username = 'Matldc2@test.com.maike' );

        User u3 = new User( Alias = 'samLdc3', 
                Email = 'ldc@test.com.maike',
                Emailencodingkey = 'UTF-8', 
                Lastname = '用户3', 
                Languagelocalekey = 'en_US',
                Localesidkey = 'en_US', 
                Profileid = ldcProfile, 
                Country = 'United States',
                IsActive = true,
                UserDepartment__c='客户服务中心(产品培训部)',
                Timezonesidkey = 'America/Los_Angeles', 
                EmployeeNumber = '123453',
                CompanyName = 'Y-MKSY',
                Province__c = '四川省',
                FederationIdentifier = '123453',

                Username = 'Matldc3@test.com.maike' );
        User u4 = new User( Alias = 'samLdc4', 
                Email = 'ldc@test.com.maike',
                Emailencodingkey = 'UTF-8', 
                Lastname = '工程师1', 
                Languagelocalekey = 'en_US',
                Localesidkey = 'en_US', 
                Profileid = engProfile, 
                Country = 'United States',
                CompanyName = 'Y-MKSY',
                Province__c = '四川省',
                FederationIdentifier = '123454',
                IsActive = true,
                UserDepartment__c='客户服务中心(工程部)',
                Timezonesidkey = 'America/Los_Angeles', 
                EmployeeNumber = '123454',
                Username = 'Matldc4@test.com.maike' );
        
        insert new List<User>{u1,u2,u3,u4};

        User user1 = [SELECT Id,Name FROM User WHERE Name = '用户1'];
        User user2 = [SELECT Id,Name FROM User WHERE Name = '用户2'];
        User engineer = [SELECT Id,Name FROM User WHERE Name = '用户3'];

        //创建客户
        Account acc1 = TestUtils.createAccount();
        acc1.Name = '测试客户A001';
        acc1.AccountNumber = 'AN.0001';
        acc1.Level__c = '一级医院';

        Account acc2 = TestUtils.createAccount();
        acc2.Name = '测试客户B002';
        acc2.Level__c = '一级医院';
        acc2.AccountNumber = 'AN.0002';
        insert new List<Account>{acc1,acc2};
        //创建产品
        Product__c pro1 = TestUtils.createProduct();
        pro1.Name = '尿有形成份检测稀释液UPS-1';
        pro1.NCCode__c = 'P-0001';

        Product__c pro2 = TestUtils.createProduct();
        pro2.Name = '尿有形成份检测稀释液UPS-2';
        pro2.NCCode__c = 'P-0002';

        Product__c pro3 = TestUtils.createProduct();
        pro3.Name = '尿有形成份检测稀释液UPS-3';
        pro3.NCCode__c = 'P-0003';
        insert new List<Product__c>{pro1,pro2,pro3};
        //创建联系人
        Contact con = TestUtils.createContact();
        con.MobilePhone = '13588885555';
        con.Post__c = '科长/主任';

        //创建合同
        Contract__c ctr = TestUtils.createContract();
        ctr.Account__r = acc1;
        //创建订单
        Order__c ord = TestUtils.createOrder();
        ord.Account__c = acc1.Id;
        ord.Contact__c = con.Id;
        ord.Contract__c = ctr.Id;
        ord.Name = 'OP.2017010001';
        ord.NCsale_hid__c = '1';
        insert ord;

        //创建订单明细 
        Order_Product__c op1 = new Order_Product__c();
        op1.NCsalecode__c = 'OP.2017010001';
        op1.Order__c = ord.Id;
        op1.Original_Price__c = 21;
        op1.Quote_Price__c = 25;
        op1.Product__c = pro1.Id;
        op1.NCsale_bid__c = '1';

        Order_Product__c op2 = new Order_Product__c();
        op2.NCsalecode__c = 'OP.2017010001';
        op2.Order__c = ord.Id;
        op2.Original_Price__c = 43;
        op2.Quote_Price__c = 56;
        op2.Product__c = pro2.Id;
        op2.NCsale_bid__c = '2';

        Order_Product__c op3 = new Order_Product__c();
        op3.NCsalecode__c = 'OP.2017010001';
        op3.Order__c = ord.Id;
        op3.Original_Price__c = 11;
        op3.Quote_Price__c = 55;
        op3.Product__c = pro3.Id;
        op3.NCsale_bid__c = '3';
        insert new List<Order_Product__c>{op1,op2,op3};

        //开始页面测试
        Test.startTest();

        PageReference page = new PageReference('/apex/SignaturePage?btn=EngineerSign');//EngineerSign - UserDepartmen - ManageDepartment
        System.Test.setCurrentPage(page);
        SignatureController sc = new SignatureController(new ApexPages.StandardController(ord));
        sc = new SignatureController(new ApexPages.StandardController(ord));
        Blob img = Blob.valueOf('这是一个图片测试数据1');
        sc.signStr = 'data:image/png;base64,' + EncodingUtil.base64Encode(img);
        sc.saveItem();

        page = new PageReference('/apex/SignaturePage?btn=UserDepartmen');//EngineerSign - UserDepartmen - ManageDepartment
        System.Test.setCurrentPage(page);
        ord.Signed__c = true;
        update ord;
        sc = new SignatureController(new ApexPages.StandardController(ord));
        img = Blob.valueOf('这是一个图片测试数据1');
        sc.signStr = 'data:image/png;base64,' + EncodingUtil.base64Encode(img);
        sc.saveItem();


        page = new PageReference('/apex/SignaturePage?btn=ManageDepartment');//EngineerSign - UserDepartmen - ManageDepartment
        System.Test.setCurrentPage(page);
        sc = new SignatureController(new ApexPages.StandardController(ord));
        img = Blob.valueOf('这是一个图片测试数据1');
        sc.signStr = 'data:image/png;base64,' + EncodingUtil.base64Encode(img);
        sc.saveItem();

        Test.stopTest();
    }
    
    static testMethod void testMethod2() {
        Id ldcProfile = [Select Id From Profile Where Name ='系统管理员' or Name='System Administrator'].Id;
        Id engProfile = [Select Id From Profile Where Name ='工程部工程师'].Id;
        User u1 = new User( Alias = 'samLdc1', 
                         Email = 'ldc@test.com.maike',
                         Emailencodingkey = 'UTF-8', 
                         Lastname = '用户1', 
                         Languagelocalekey = 'en_US',
                         Localesidkey = 'en_US', 
                         Profileid = ldcProfile, 
                         Country = 'United States',
                         IsActive = true,
                         SubCompany__c='四川省迈克实业有限公司',
                         UserDepartment__c='客户服务中心(工程部)',
                         Timezonesidkey = 'America/Los_Angeles', 
                         EmployeeNumber = '123451',
                         CompanyName = 'Y-MKSY',
                         Province__c = '四川省',
                         FederationIdentifier = '123451',
                         Username = 'Matldc1@test.com.maike' );
        
        User u2 = new User( Alias = 'samLdc2', 
                Email = 'ldc@test.com.maike',
                Emailencodingkey = 'UTF-8', 
                Lastname = '用户2', 
                Languagelocalekey = 'en_US',
                Localesidkey = 'en_US', 
                Profileid = ldcProfile, 
                Country = 'United States',
                IsActive = true,
                UserDepartment__c='客户服务中心(产品培训部)',
                Timezonesidkey = 'America/Los_Angeles', 
                EmployeeNumber = '123452',
                CompanyName = 'Y-MKSY',
                Province__c = '四川省',
                FederationIdentifier = '123452',
                Username = 'Matldc2@test.com.maike' );
        
        User u3 = new User( Alias = 'samLdc3', 
                Email = 'ldc@test.com.maike',
                Emailencodingkey = 'UTF-8', 
                Lastname = '用户3', 
                Languagelocalekey = 'en_US',
                Localesidkey = 'en_US', 
                Profileid = ldcProfile, 
                Country = 'United States',
                IsActive = true,
                UserDepartment__c='客户服务中心(产品培训部)',
                Timezonesidkey = 'America/Los_Angeles', 
                EmployeeNumber = '123453',
                CompanyName = 'Y-MKSY',
                Province__c = '四川省',
                FederationIdentifier = '123453',

                Username = 'Matldc3@test.com.maike' );
        User u4 = new User( Alias = 'samLdc4', 
                Email = 'ldc@test.com.maike',
                Emailencodingkey = 'UTF-8', 
                Lastname = '工程师1', 
                Languagelocalekey = 'en_US',
                Localesidkey = 'en_US', 
                Profileid = engProfile, 
                Country = 'United States',
                CompanyName = 'Y-MKSY',
                FederationIdentifier = '123454',
                IsActive = true,
                Province__c = '四川省',
                UserDepartment__c='客户服务中心(工程部)',
                Timezonesidkey = 'America/Los_Angeles', 
                EmployeeNumber = '123454',
                Username = 'Matldc4@test.com.maike' );
        
        insert new List<User>{u1,u2,u3,u4};

        User user1 = [SELECT Id,Name FROM User WHERE Name = '用户1'];
        User user2 = [SELECT Id,Name FROM User WHERE Name = '用户2'];
        User engineer = [SELECT Id,Name FROM User WHERE Name = '用户3'];

        RecordType rt = [select Id from RecordType where SObjectType = 'WorkOrder__c' and DeveloperName = 'InstallationOrder'];

        //创建客户
        Account acc1 = TestUtils.createAccount();
        acc1.Name = '测试客户A001';
        acc1.AccountNumber = 'AN.0001';
        acc1.Level__c = '一级医院';

        Account acc2 = TestUtils.createAccount();
        acc2.Name = '测试客户B002';
        acc2.AccountNumber = 'AN.0002';
        acc2.Level__c = '一级医院';
        insert new List<Account>{acc1,acc2};
        
        Contact c1 = TestUtils.createContact();
        c1.AccountId = acc1.Id;
        c1.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('TerminalCustomer', 'Contact');

        insert c1;

        Product__c pro1 = TestUtils.createInst();
        pro1.RecordTypeId =Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');
        insert pro1;
        
       // String smsTemplate = Utils.getSMSTemplate('个案受理');
        Case cas = new Case();
        cas.Manufacturer__c = pro1.Id;
        cas.AccountId = acc1.Id;
        insert cas;
       ///13:57:52:734 VF_PAGE_MESSAGE 客户名称、使用科室负责人、安装仪器名称、机身号、仪器软件版本、
        ///安装情况的1)至10)、仪器安装调试情况、说明为必填，状态和服务开始时间(即点击抵达按钮)不能为空
        WorkOrder__c wo = new WorkOrder__c();
        wo.Case__c = cas.Id;
        wo.Account__c = acc1.Id;
        wo.ManufactureInfo__c = pro1.Id;
        // wo.EndProcessingTime__c = Date.newInstance(2020, 1, 1);
        
        wo.IMEINumber__c = '00001';
        wo.Software_Version__c = 'test';
        wo.Software_Name__c = 'test';
        wo.Reporter_Version__c = 'test';
        wo.NormalRequirement1__c = 'test';
        wo.NormalRequirement2__c = 'test';
        wo.NormalRequirement3__c = 'test';
        wo.NormalRequirement4__c = 'test';
        wo.NormalRequirement5__c = 'test';
        wo.NormalRequirement6__c = 'test';
        wo.NormalRequirement7__c = 'test';
        wo.NormalRequirement8__c = 'test';
        wo.NormalRequirement9__c = 'test';
        wo.NormalRequirement10__c = 'test';
        wo.ActualSituation1__c = 1;
        wo.ActualSituation2__c = 2;
        wo.ActualSituation3__c = '是';
        wo.ActualSituation4__c = 4;
        wo.ActualSituation5__c = 5;
        wo.ActualSituation6__c = 6;
        wo.ActualSituation7__c = 7;
        wo.ActualSituation8__c = '是';
        wo.ActualSituation9__c = '是';
        wo.ActualSituation10__c = '是';
        wo.IsMeetRequirement1__c = true;
        wo.IsMeetRequirement2__c = true;
        wo.IsMeetRequirement3__c = true;
        wo.IsMeetRequirement4__c = true;
        wo.IsMeetRequirement5__c = true;
        wo.IsMeetRequirement6__c = true;
        wo.IsMeetRequirement7__c = true;
        wo.IsMeetRequirement8__c = true;
        wo.IsMeetRequirement9__c = true;
        wo.IsMeetRequirement10__c = true;
        
       	wo.UserDepartmentContact__c = c1.Id;
        wo.InstallationDebugging__c = 'test';
        
        wo.Instruction__c = 'test';
        wo.DepartureTime__c = Datetime.newInstance(2020, 1, 1).addDays(-1);
        wo.Install_Start_Datetime__c = Datetime.newInstance(2020, 1, 1);
        wo.CenterExecutiveManager__c = user1.Id;
        wo.SalesDeptAreaManager__c = user1.Id;
        wo.FinancialManager__c = user1.Id;
        wo.Technician__c = user1.Id;
        wo.Status__c = '处理中';
        wo.RecordTypeId = rt.Id;
        
        wo.Terminal__c = 'Molex';
        wo.InstrumentVersion__c = 'plus';
        wo.WhetherToMakeASingle__c = '否';
        wo.ManagementDepContact__c = c1.Id;
        insert wo;
        
        

        WorkOrderTeam__c wt = new WorkOrderTeam__c();
        wt.User__c = user1.Id;
        wt.WorkOrder__c = wo.Id;

        WorkOrderTeam__c wt1 = new WorkOrderTeam__c();
        wt1.User__c = user2.Id;
        wt1.WorkOrder__c = wo.Id;

        insert new List<WorkOrderTeam__c>{wt,wt1};

        //开始页面测试
        Test.startTest();

        PageReference page = new PageReference('/apex/SignaturePage?btn=UserDepartmen');
        System.Test.setCurrentPage(page);
        wo.Signed_UsedDepartmen__c = false;
        wo.Status__c = '处理中';
        wo.FullIn_Completed__c = true;

        update wo;
        SignatureController sc = new SignatureController(new ApexPages.StandardController(wo));
        Blob img = Blob.valueOf('这是一个图片测试数据1');
        sc.signStr = 'data:image/png;base64,' + EncodingUtil.base64Encode(img);
        sc.saveItem();
/*
        page = new PageReference('/apex/SignaturePage?btn=EngineerSign');
        System.Test.setCurrentPage(page);
        wo.Signed_UsedDepartmen__c = true;
        wo.Signed_Engineer__c = false;
        sc = new SignatureController(new ApexPages.StandardController(wo));
        img = Blob.valueOf('这是一个图片测试数据1');
        sc.signStr = 'data:image/png;base64,' + EncodingUtil.base64Encode(img);
        sc.saveItem();

        page = new PageReference('/apex/SignaturePage?btn=UserDepartmen');
        System.Test.setCurrentPage(page);
        wo.Signed_UsedDepartmen__c = true;
        sc = new SignatureController(new ApexPages.StandardController(wo));
        img = Blob.valueOf('这是一个图片测试数据1');
        sc.signStr = 'data:image/png;base64,' + EncodingUtil.base64Encode(img);
        // sc.saveItem();
        */

        Test.stopTest();
        

    }
}